package itcast.bos.base.action;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.orm.hibernate3.support.OpenSessionInViewFilter;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import itcast.bos.base.service.IAreaService;
import itcast.bos.domain.base.Area;

@Controller
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
public class AreaAction extends ActionSupport implements ModelDriven<Area> {
	private File file;
	private String fileContentName;
	private String fileType;

	@Autowired
	private IAreaService is;
	
	private int page;
	private int rows;
	private Area area=new Area();
	
	@Action(value="exportFile",results={@Result(name="success",type="redirect",location="./pages/base/area.html")})
	public String exporrt() {
		System.out.println("下载文件");
		return SUCCESS;
	}
	
	
	
	
	
	@Action(value="queryArea",results={@Result(name="success",type="json")})
	public String findArea() {
		Pageable pageable=new PageRequest(page-1, rows);
		
		Specification<Area> sf=new Specification<Area>() {
			
			@Override
			public Predicate toPredicate(Root<Area> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				System.out.println(area);
				List<Predicate> list=new ArrayList<Predicate>();
				if(StringUtils.isNotBlank(area.getProvince())){
					Predicate p1 = cb.like(root.get("province").as(String.class), "%"+area.getProvince()+"%");
					list.add(p1);
				}
				if(StringUtils.isNotBlank(area.getCity())){
					Predicate p2 = cb.like(root.get("city").as(String.class), "%"+area.getCity()+"%");
					list.add(p2);
				}
				if(StringUtils.isNotBlank(area.getDistrict())){
					Predicate p3 = cb.like(root.get("district").as(String.class), "%"+area.getDistrict()+"%");
					list.add(p3);
				}
				System.out.println("list"      +list);
				System.out.println(cb.and(list.toArray(new Predicate[0])));
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		
		Page<Area> page=is.findArea(sf,pageable);
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("total",page.getTotalElements() );
		map.put("rows", page.getContent());
		ActionContext.getContext().getValueStack().push(map);
		return SUCCESS;
	}

	@Action(value = "area_batchImport")
	public String upload() throws IOException {
		System.out.println("上傳文件。。。");
		// 新建一个集合
		List<Area> list = new ArrayList<Area>();
		// 基于.xls来解析
		// 新建工作簿
		HSSFWorkbook wk = new HSSFWorkbook(new FileInputStream(file));
		HSSFSheet sheet = wk.getSheetAt(0); // 通过索引来获取 sheet
		// 遍历sheet
		for (Row row : sheet) {
			// 跳过第一行
			if (row.getRowNum() == 0) {
				continue;
			}
			// 跳过空行
			if (row.getCell(0) == null || StringUtils.isBlank(row.getCell(0).getStringCellValue())) {
				continue;
			}
			// 填充。。。。
			System.out.println(row);
			Area area = new Area();
			area.setId(row.getCell(0).getStringCellValue());
			area.setProvince(row.getCell(1).getStringCellValue());
			area.setCity(row.getCell(2).getStringCellValue());
			area.setDistrict(row.getCell(3).getStringCellValue());
			area.setPostcode(row.getCell(4).getStringCellValue());
			// 添加到集合中
			list.add(area);
		}
		is.save(list);
		return NONE;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}
	
	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public String getFileContentName() {
		return fileContentName;
	}

	public void setFileContentName(String fileContentName) {
		this.fileContentName = fileContentName;
	}

	public String getFileType() {
		return fileType;
	}

	public void setFileType(String fileType) {
		this.fileType = fileType;
	}
	@Override
	public Area getModel() {
		return area;
	}

}
